<template>
  <div class="login_container">
    <div class="login_box">
      <!--      logo区域-->
      <div class="login_logo"></div>
      <!--登录表单区域-->
      <el-form :model="user" :rules="formRules" ref="formRef">
        <!--用户名-->
        <el-form-item prop="mobile">
          <el-input
            v-model="user.mobile"
            prefix-icon="el-icon-user"
            placeholder="请输入手机号"
          ></el-input>
          <!--验证码-->
        </el-form-item>
        <el-form-item prop="code">
          <el-input
            v-model="user.code"
            prefix-icon="el-icon-lock"
            placeholder="请输入验证码"
            :type="passwordType"
          >
          </el-input>
          <span
            v-if="passwordType === 'password'"
            @click="showpaddword"
            class="showeyes"
          >
            <!-- <i class="iconfont icon-eye-no"></i> -->
            <svg class="icon" aria-hidden="true">
              <use xlink:href="#icon-eye-no"></use>
            </svg>
          </span>

          <span v-else @click="showpaddword" class="showeyes">
            <!-- <i class="iconfont icon-eye-on"></i> -->
            <svg class="icon" aria-hidden="true">
              <use xlink:href="#icon-eye-on"></use>
            </svg>
          </span>
        </el-form-item>

        <!-- <el-form-item> -->
        <!-- 验证码验证登录 -->
        <div class="tokenpassword">
          <vab-verify
            ref="slideDiv"
            :w="400"
            :slider-text="text"
            :h="175"
            @success="handleSuccess"
            @fail="handleError"
          ></vab-verify>
        </div>

        <!--验证码-->
        <!-- </el-form-item> -->

        <!--        是否同意协议-->
        <el-form-item prop="agree">
          <el-checkbox v-model="user.agree"
            >我已阅读并同意用户协议和隐私条款</el-checkbox
          >
        </el-form-item>
        <!--        按钮-->
        <el-form-item>
          <el-button type="primary" round @click="onLogin" :loading="isLoading"
            >登录</el-button
          >
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import VabVerify from "@/plugins/vabVerify";
export default {
  name: "Login",
  components: {
    VabVerify,
  },
  data() {
    return {
      text: "向右滑动",
      icon: "iconfont icon-eye-on",
      icon1: "iconfont icon-eye-no",
      user: {
        mobile: "13911111111",
        code: "246810",
        // mobile: "",
        // code: "",
        agree: false,
      },
      passwordType: "password",
      isLoading: false,
      formRules: {
        mobile: [
          {
            required: true,
            message: "手机号不能为空",
            trigger: "blur",
          },
          {
            pattern:
              /^(13[0-9]|14[01456879]|15[0-3,5-9]|16[2567]|17[0-8]|18[0-9]|19[0-3,5-9])\d{8}$/,
            message: "请输入正确的手机号格式",
            trigger: "blur",
          },
        ],
        code: [
          {
            required: true,
            message: "验证码不能为空",
            trigger: "blur",
          },
          {
            pattern: /^\d{6}$/,
            message: "请输入正确的验证码格式",
          },
        ],
        agree: [
          {
            validator: (rule, value, callback) => {
              if (!value) {
                callback(new Error("请勾选同意用户协议"));
              } else {
                callback();
              }
            },
            trigger: "change",
          },
        ],
      },
      redirect: undefined,
    };
  },

  methods: {
    onLogin() {
      // // 表单验证
      // this.$refs.formRef.validate((valid) => {
      //   // 如果表单验证失败，停止请求提交
      //   if (!valid) {
      //     return;
      //   }
      //   // 验证通过,提交登录

      //   this.login();
      // });
      this.handleSuccess();
    },
    // 验证码登录
    handleSuccess() {
      // this.$baseMessage('校验成功', 'success')
      // var vm = this;
      this.$message({
        showClose: true,
        message: "验证成功！",
        type: "success",
      });

      this.$refs.formRef.validate((valid) => {
        // 如果表单验证失败，停止请求提交
        if (valid) {
          // 打开loading
          this.isLoading = true;

          // 借用vuex实现的登录方式
          this.$store
            .dispatch("user/login", this.user)
            .then(() => {
              // console.log(465515133);
              this.$router.push("/").catch(() => {});
              this.isLoading = false;
            })
            .catch(() => {
              this.isLoading = false;
            });
        } else {
          console.log("信息未填写完整");
          return false;
        }
      
      });
    },

    handleError() {
      this.$baseMessage("校验失败", "error");
    },
    // login() {
    //   // 获取表单数据
    //   // const user = this.user

    //   // 打开loading
    //   this.isLoading = true;

    //   // 借用vuex实现的登录方式
    //   this.$store.dispatch("user/login", this.user).then(() => {
    //     // console.log(465515133);
    //     this.$router.push("/").catch(() => {});
    //     this.isLoading = false;
    //   });

    //   // 传统的登录方式
    //   // login(this.user)
    //   //   .then((res) => {
    //   //     console.log(res);
    //   //     this.$message.success("登录成功");
    //   //     // 保存用户token到本地
    //   //     window.localStorage.setItem("user", JSON.stringify(res.data.data));
    //   //     // 成功后关闭loading
    //   //     this.isLoading = false;
    //   //     // 跳转到首页
    //   //     this.$router.push("/");
    //   //   })
    //   //   .catch((res) => {
    //   //     this.$message.error("用户名或者验证码错误");
    //   //     // console.log(res)
    //   //     // 失败后关闭loading
    //   //     this.isLoading = false;
    //   //   });
    // },
    showpaddword() {
      if (this.passwordType == "password") {
        this.passwordType = "";
      } else {
        this.passwordType = "password";
      }
    },
  },
};
</script>

<style lang="less" scoped>
.login_container {
  background: url("./login_bg.jpg");
  background-size: cover;
  height: 100%;

  .login_logo {
    height: 57px;
    // background: url("./logo_index.png") no-repeat center center;
    margin-bottom: 15px;
  }

  .login_box {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    background-color: #fff;
    padding: 50px;
    min-width: 400px;
    border-radius: 10px;
    box-shadow: 1px 0 0 rgba(0, 0, 0, 0.3);
    // .tokenpassword{
    //   padding-left: 1px;
    // }
  /deep/.slide-verify-slider[data-v-53d79ea0]{
    width: 400px;
  }

    .el-button {
      width: 100%;
    }
  }
  .showeyes {
    position: absolute;
    top: 0;
    right: 5px;
  }
}
</style>
